<?php
	require("user_settings.php"); 
	$uname="";$passwd="";
	if(isset($_GET['uname']))
		$uname=htmlspecialchars($_GET['uname']);
	if(isset($_GET['passwd']))
		$passwd=htmlspecialchars($_GET['passwd']);

	if(strlen($passwd) == 0) {
		echo "<script lang=javascript> document.location.href=(\"comp_admin.php?err='密碼是空的'\");</script>";
		exit ;
	}
	if(strlen($uname) == 0) {
		echo "<script lang=javascript> document.location.href=(\"comp_admin.php?err='使用者是空的'\");</script>";
		exit ;
	}

	#$users = json_decode(file_get_contents('passwd.json'),true);
	$ret = verifyUser($uname,$passwd);
    #if(!isset($users[$uname])) {
	if($ret == "ERR_USER_NONEXIST") {
		#echo "user $uname doesn't exist.<BR>";
		#$users[$uname] = $passwd;
		#file_put_contents("passwd.json",json_encode($users));
		#echo "$uname is registerd<BR>";
		#mkdir("users/".$uname);
		#touch("users/".$uname."/settings.json");
		##echo "<script lang=javascript> document.location.href=(\"topframe.php?local=$uname\");</script>";
		#session_start();
		#$_SESSION["login"] = true;
		#$_SESSION["id"] = "$uname";
		echo "<script lang=javascript> document.location.href=(\"comp_admin.php?err='使用者 $uname 不存在'\");</script>";
		
	}
	else {
		#echo "$uname is  set<BR>";
		
		
		if($ret == "ERR_USER_PASSWD_MISMATCH") {
			echo "<script lang=javascript> document.location.href=(\"comp_admin.php?err='密碼錯誤'\");</script>";
			#echo "abc";
		}
		else if($ret == "OK_OK")  {
			#echo "password is correct<BR>";
			session_save_path("./.php_session");
			session_start();
			$_SESSION["login"] = true;
			$_SESSION["id"] = "$uname";
			echo "<script lang=javascript> document.location.href=(\"topframe.php\");</script>";
			#echo "<script lang=javascript> document.location.href=(\"init_settings.php\");</script>";

			
		}
	}

	
?>
